home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / perl5 / Socket.z / Socket
Encoding:
Text File  |  2002-10-03  |  7.9 KB  |  265 lines

  1.  
  2.  
  3.  
  4. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C
  10.      socket.h defines and structure manipulators
  11.  
  12. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  13.          use Socket;
  14.  
  15.          $proto = getprotobyname('udp');
  16.          socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
  17.          $iaddr = gethostbyname('hishost.com');
  18.          $port = getservbyname('time', 'udp');
  19.          $sin = sockaddr_in($port, $iaddr);
  20.          send(Socket_Handle, 0, 0, $sin);
  21.  
  22.          $proto = getprotobyname('tcp');
  23.          socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
  24.          $port = getservbyname('smtp', 'tcp');
  25.          $sin = sockaddr_in($port,inet_aton("127.1"));
  26.          $sin = sockaddr_in(7,inet_aton("localhost"));
  27.          $sin = sockaddr_in(7,INADDR_LOOPBACK);
  28.          connect(Socket_Handle,$sin);
  29.  
  30.          ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
  31.          $peer_host = gethostbyaddr($iaddr, AF_INET);
  32.          $peer_addr = inet_ntoa($iaddr);
  33.  
  34.          $proto = getprotobyname('tcp');
  35.          socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
  36.          unlink('/tmp/usock');
  37.          $sun = sockaddr_un('/tmp/usock');
  38.          connect(Socket_Handle,$sun);
  39.  
  40.  
  41. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  42.      This module is just a translation of the C _s_o_c_k_e_t._h file.  Unlike the old
  43.      mechanism of requiring a translated _s_o_c_k_e_t._p_h file, this uses the hhhh2222xxxxssss
  44.      program (see the Perl source distribution) and your native C compiler.
  45.      This means that it has a far more likely chance of getting the numbers
  46.      right.  This includes all of the commonly used pound-defines like
  47.      AF_INET, SOCK_STREAM, etc.
  48.  
  49.      Also, some common socket "newline" constants are provided: the constants
  50.      CR, LF, and CRLF, as well as $CR, $LF, and $CRLF, which map to \015,
  51.      \012, and \015\012.  If you do not want to use the literal characters in
  52.      your programs, then use the constants provided here.  They are not
  53.      exported by default, but can be imported individually, and with the :crlf
  54.      export tag:
  55.  
  56.          use Socket qw(:DEFAULT :crlf);
  57.  
  58.      In addition, some structure manipulation functions are available:
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  71.  
  72.  
  73.  
  74.      inet_aton HOSTNAME
  75.           Takes a string giving the name of a host, and translates that to the
  76.           4-byte string (structure). Takes arguments of both the
  77.           'rtfm.mit.edu' type and '18.181.0.24'. If the host name cannot be
  78.           resolved, returns undef. For multi-homed hosts (hosts with more than
  79.           one address), the first address found is returned.
  80.  
  81.      inet_ntoa IP_ADDRESS
  82.           Takes a four byte ip address (as returned by _i_n_e_t__a_t_o_n()) and
  83.           translates it into a string of the form 'd.d.d.d' where the 'd's are
  84.           numbers less than 256 (the normal readable four dotted number
  85.           notation for internet addresses).
  86.  
  87.      INADDR_ANY
  88.           Note: does not return a number, but a packed string.
  89.  
  90.           Returns the 4-byte wildcard ip address which specifies any of the
  91.           hosts ip addresses. (A particular machine can have more than one ip
  92.           address, each address corresponding to a particular network
  93.           interface. This wildcard address allows you to bind to all of them
  94.           simultaneously.)  Normally equivalent to _i_n_e_t__a_t_o_n('0.0.0.0').
  95.  
  96.      INADDR_BROADCAST
  97.           Note: does not return a number, but a packed string.
  98.  
  99.           Returns the 4-byte 'this-lan' ip broadcast address.  This can be
  100.           useful for some protocols to solicit information from all servers on
  101.           the same LAN cable.  Normally equivalent to
  102.           _i_n_e_t__a_t_o_n('255.255.255.255').
  103.  
  104.      INADDR_LOOPBACK
  105.           Note - does not return a number.
  106.  
  107.           Returns the 4-byte loopback address. Normally equivalent to
  108.           _i_n_e_t__a_t_o_n('localhost').
  109.  
  110.      INADDR_NONE
  111.           Note - does not return a number.
  112.  
  113.           Returns the 4-byte 'invalid' ip address. Normally equivalent to
  114.           _i_n_e_t__a_t_o_n('255.255.255.255').
  115.  
  116.      sockaddr_in PORT, ADDRESS
  117.  
  118.      sockaddr_in SOCKADDR_IN
  119.           In an array context, unpacks its SOCKADDR_IN argument and returns an
  120.           array consisting of (PORT, ADDRESS).  In a scalar context, packs its
  121.           (PORT, ADDRESS) arguments as a SOCKADDR_IN and returns it.  If this
  122.           is confusing, use _p_a_c_k__s_o_c_k_a_d_d_r__i_n() and _u_n_p_a_c_k__s_o_c_k_a_d_d_r__i_n()
  123.           explicitly.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  137.  
  138.  
  139.  
  140.      pack_sockaddr_in PORT, IP_ADDRESS
  141.           Takes two arguments, a port number and a 4 byte IP_ADDRESS (as
  142.           returned by _i_n_e_t__a_t_o_n()). Returns the sockaddr_in structure with
  143.           those arguments packed in with AF_INET filled in.  For internet
  144.           domain sockets, this structure is normally what you need for the
  145.           arguments in _b_i_n_d(), _c_o_n_n_e_c_t(), and _s_e_n_d(), and is also returned by
  146.           _g_e_t_p_e_e_r_n_a_m_e(), _g_e_t_s_o_c_k_n_a_m_e() and _r_e_c_v().
  147.  
  148.      unpack_sockaddr_in SOCKADDR_IN
  149.           Takes a sockaddr_in structure (as returned by _p_a_c_k__s_o_c_k_a_d_d_r__i_n())
  150.           and returns an array of two elements: the port and the 4-byte ip-
  151.           address.  Will croak if the structure does not have AF_INET in the
  152.           right place.
  153.  
  154.      sockaddr_un PATHNAME
  155.  
  156.      sockaddr_un SOCKADDR_UN
  157.           In an array context, unpacks its SOCKADDR_UN argument and returns an
  158.           array consisting of (PATHNAME).  In a scalar context, packs its
  159.           PATHNAME arguments as a SOCKADDR_UN and returns it.  If this is
  160.           confusing, use _p_a_c_k__s_o_c_k_a_d_d_r__u_n() and _u_n_p_a_c_k__s_o_c_k_a_d_d_r__u_n()
  161.           explicitly.  These are only supported if your system has <_s_y_s/_u_n._h>.
  162.  
  163.      pack_sockaddr_un PATH
  164.           Takes one argument, a pathname. Returns the sockaddr_un structure
  165.           with that path packed in with AF_UNIX filled in. For unix domain
  166.           sockets, this structure is normally what you need for the arguments
  167.           in _b_i_n_d(), _c_o_n_n_e_c_t(), and _s_e_n_d(), and is also returned by
  168.           _g_e_t_p_e_e_r_n_a_m_e(), _g_e_t_s_o_c_k_n_a_m_e() and _r_e_c_v().
  169.  
  170.      unpack_sockaddr_un SOCKADDR_UN
  171.           Takes a sockaddr_un structure (as returned by _p_a_c_k__s_o_c_k_a_d_d_r__u_n())
  172.           and returns the pathname.  Will croak if the structure does not have
  173.           AF_UNIX in the right place.
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. SSSSoooocccckkkkeeeetttt((((3333))))                                                            SSSSoooocccckkkkeeeetttt((((3333))))
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.                                                                         PPPPaaaaggggeeee 4444
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.